<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加告警明细' : '编辑告警明细'" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="24">
            <el-form-item label="告警类型：" prop="alarmType">
              <el-input v-model="ruleForm.alarmType" placeholder="请输入告警类型" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="推送方式：" prop="pushMode"><el-select multiple @change="getPush" v-model="ruleForm.pushMode" placeholder="请选择推送方式" clearable>
                <el-option label="邮件" value="0"></el-option>
                <el-option label="短信" value="1"></el-option>
                <el-option label="语音" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="开始值：" prop="startValue">
              <el-input-number v-model="ruleForm.startValue" placeholder="请输入开始值" :precision="2" :step="1" :max="9999999" :min="0" :disabled="seeType"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="结束值：" prop="endValue">
              <el-input-number v-model="ruleForm.endValue" placeholder="请输入结束值" :precision="2" :step="1" :max="9999999" :min="0" :disabled="seeType"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="告警颜色：" prop="colorType">
              <el-color-picker v-model="ruleForm.colorType" :disabled="seeType"></el-color-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getModelAlarmDetail, saveModelAlarmDetail } from '@/api/alarm'

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: ''
    },
    addEditStatus: {
      type: String,
      default: 'add'
    },
    addEditIsDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: '',
        modelAlarmSettingsId: '',
        alarmType: '',
        startValue: undefined,
        endValue: undefined,
        colorType: '',
        pushMode: ''
      },
      rules: {
        // 表单规则
        alarmType: [{ required: true, message: '请输入告警类型', trigger: 'blur' }],
        startValue: [{ required: false, message: '请输入开始值', trigger: 'blur' }],
        endValue: [{ required: false, message: '请输入结束值', trigger: 'blur' }],
        colorType: [{ required: true, message: '请选择告警颜色', trigger: 'change' }]
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false // 是否禁用
    }
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == 'add') {
          this.ruleForm.id = ''
          this.seeType = false
        }

        // 编辑
        if (this.addEditStatus == 'edit') {
          this.ruleForm.id = this.addEditId
          this.seeType = false
          this.getDetail()
        }

        // 查看
        if (this.addEditStatus == 'see') {
          this.ruleForm.id = this.addEditId
          this.seeType = true
          this.getDetail()
        }

        this.ruleForm.modelAlarmSettingsId = this.$route.query.modelAlarmSettingsId
      }
    }
  },
  methods: {
    getPush(val) {
      console.log(val)
    },
    // 详情
    getDetail() {
      getModelAlarmDetail({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          if (res.data.pushMode) {
            res.data.pushMode = res.data.pushMode.split(',')
          }
          if (res.data.startValue == null) {
            res.data.startValue = undefined
          }
          if (res.data.endValue == null) {
            res.data.endValue = undefined
          }

          this.ruleForm = res.data
        }
      })
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.ruleForm.startValue == undefined && this.ruleForm.endValue == undefined) {
            this.$message({
              type: 'error',
              message: '开始值和结束值至少有一个不能为空'
            })
            return
          }

          if (this.ruleForm.startValue != undefined && this.ruleForm.endValue != undefined && this.ruleForm.startValue > this.ruleForm.endValue) {
            this.$message({
              type: 'error',
              message: '开始值不能大于结束值'
            })
            return
          }
          this.ruleForm.pushMode = this.ruleForm.pushMode.join(',')

          saveModelAlarmDetail(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })

              // 关闭弹窗
              this.$parent.closeAddEdit()
              // 刷新列表
              this.$parent.onSubmit()
            }
          })
        }
      })
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.$parent.closeAddEdit()
    }
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }
}
</style>
